package com.ruoyi.web.controller.index;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.activity.service.IActivityService;
import com.ruoyi.activity.service.IContestService;
import com.ruoyi.blog.service.BlogService;
import com.ruoyi.common.config.MyThreadPoolConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.model.activity.Activity;
import com.ruoyi.model.blog.Blog;
import com.ruoyi.model.contest.Contest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author : 张怀秋
 * 首页接口
 **/
@Api(tags = "首页接口")
@Slf4j
@RestController
@RequestMapping("/api/index")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IndexContestController extends BaseController {

    /**
     * 自定义线程池
     */
    private final MyThreadPoolConfig config;

    /**
     * 竞赛服务
     */
    private final IContestService contestService;

    /**
     * 活动服务
     */
    private final IActivityService activityService;

    /**
     * 文章服务
     */
    private final BlogService blogService;

    @ApiOperation("查询首页全部数据")
    @GetMapping("/index-data")
    public AjaxResult findIndexData() {
        Map<String, Object> map = new HashMap<>(5);
        //获取自定义线程池
        ThreadPoolExecutor executor = config.threadPoolExecutor();

        //查询首页的默认竞赛数据
        CompletableFuture<Void> contest = CompletableFuture.runAsync(() -> {
            List<Contest> indexContestList = contestService.queryIndexContest();
            map.put("indexContestList", indexContestList);
        }, executor);

        //查询首页默认活动数据
        CompletableFuture<Void> activity = CompletableFuture.runAsync(() -> {
            List<Activity> indexActivityList = activityService.queryIndexActivity();
            map.put("indexActivityList", indexActivityList);
        }, executor);

        //查询首页文章活动数据
        CompletableFuture<Void> blog = CompletableFuture.runAsync(() -> {
            List<Blog> indexBlogList = blogService.queryIndexBlogMap();
            map.put("indexBlogList", indexBlogList);
        }, executor);

        CompletableFuture.allOf(contest, activity, blog).join();

        return success(map);
    }

    @ApiOperation("查询竞赛，活动，文章的数量")
    @GetMapping("/index-number")
    public AjaxResult findIndexNumber() {
        Map<String, Object> map = new HashMap<>(3);
        long contestNumber = contestService.list(Wrappers.<Contest>lambdaQuery().eq(Contest::getStatus, "0")).size();
        long activityNumber = activityService.list(Wrappers.<Activity>lambdaQuery().eq(Activity::getStatus, "0")).size();
        long blogNumber = blogService.list(Wrappers.<Blog>lambdaQuery().eq(Blog::getStatus, "0")).size();
        map.put("contestNumber", contestNumber);
        map.put("activityNumber", activityNumber);
        map.put("blogNumber", blogNumber);
        return success(map);
    }


}
